package jwdt.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlConfigUtils {
	
	public static void springConfigUpdateDao(File file,String daoId,String daoClass)throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		
		List<Element> installeds = root.elements("bean");
		for(Element element : installeds){
			if (element.attributeValue("id").equals(daoId)) {
				root.remove(element);
			}
		}
		Element bean = root.addElement("bean");
		bean.addAttribute("id", daoId);
		bean.addAttribute("class", daoClass);
		bean.addAttribute("parent", "baseDao");
				
		Writer fileWriter = new FileWriter(file);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setNewLineAfterDeclaration(false);
		XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
		xmlWriter.write(document);
		xmlWriter.flush();
		xmlWriter.close();
	}
	
	public static void springConfigUpdateService(File file,String serviceId,String serviceClass,String daoId)throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		
		List<Element> installeds = root.elements("bean");
		for(Element element : installeds){
			if (element.attributeValue("id").equals(serviceId)) {
				root.remove(element);
			}
		}
		Element bean = root.addElement("bean");
		bean.addAttribute("id", serviceId);
		bean.addAttribute("class", serviceClass);
			
		Element property = bean.addElement("property");
		property.addAttribute("name", daoId);
		property.addAttribute("ref", daoId);
						
		Writer fileWriter = new FileWriter(file);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setNewLineAfterDeclaration(false);
		XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
		xmlWriter.write(document);
		xmlWriter.flush();
		xmlWriter.close();
	}
	
	public static void ibatisConfigUpdate(File file,String resource)throws Exception{
		SAXReader saxReader = new SAXReader();
		saxReader.setValidation(false); 
		saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
		
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		
		List<Element> installeds = root.elements("sqlMap");
		for(Element element : installeds){
			if (element.attributeValue("resource").equals(resource)) {
				return;
			}
		}
		
		Element bean = root.addElement("sqlMap");
		bean.addAttribute("resource",resource);
				
		Writer fileWriter = new FileWriter(file);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setNewLineAfterDeclaration(true);
		XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
		xmlWriter.write(document);
		xmlWriter.flush();
		xmlWriter.close();
	}
	
	public static void hibernateConfigUpdate(File file,String resource)throws Exception{
		SAXReader saxReader = new SAXReader();
		saxReader.setValidation(false); 
		saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
		
		Document document = saxReader.read(file);
		Element root = document.getRootElement();
		Element sessionFactory = root.element("session-factory");
		
		List<Element> installeds = sessionFactory.elements("mapping");
		for(Element element : installeds){
			if (element.attributeValue("resource").equals(resource)) {
				return;
			}
		}
		
		Element bean = sessionFactory.addElement("mapping");
		bean.addAttribute("resource",resource);
				
		Writer fileWriter = new FileWriter(file);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setNewLineAfterDeclaration(true);
		XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
		xmlWriter.write(document);
		xmlWriter.flush();
		xmlWriter.close();
	}
}
